//
//  KunWeatherPickerVC.swift
//  KunLanCamera
//
//  Created by 李东 on 2024/3/24.
//

import UIKit

class KunWeatherPickerVC: UIViewController {
    
    
    @IBOutlet weak var bgView: UIView!
    let CellID = "weatherCell"
    lazy var weatherList = ["晴", "多云", "闪电", "多云转晴", "雨", "雪", "彩虹", "雷雨", "雾", "有风", "太阳雨", "阴天"]
    var collectView: UICollectionView!
    var selectCallBack:((String) -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .black.withAlphaComponent(0.5)
        // Do any additional setup after loading the view.
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = (CGFloat.screenWidth-60 - 55*4)/3
        layout.itemSize = CGSize(width: 55, height: 78)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
        collectView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectView.backgroundColor = .clear
        collectView.delegate = self
        collectView.dataSource = self
        collectView.register(UINib(nibName: "KunWeatheritemCell", bundle: nil), forCellWithReuseIdentifier: CellID)
        bgView.addSubview(collectView)
        collectView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(30)
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bgView.jw_corner(byRoundingCorners: [.topLeft, .topRight], radii: 30)
    }

    @IBAction func closeHandle(_ sender: Any) {
        dismiss(animated: true)
    }
    

}

extension KunWeatherPickerVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        weatherList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID, for: indexPath) as! KunWeatheritemCell
        cell.weather = weatherList[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectCallBack?(weatherList[indexPath.row])
        dismiss(animated: true)
    }
}
